package com.icss.ah.platformActivity.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.base.util.DateUtils;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.platformActivity.service.ActivityProductService;

/**
 * 平台活动商品
 *
 */
@Controller
@Scope("prototype")
public class ActivityProductAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private ActivityProductService activityProductService;
	private Json jsonText=new Json();
	
	private String activityUuid;//活动uuid
	private String minPriceStr;//商品最低价格
	private String maxPriceStr;//商品最高价格
	private String categoryCode;//商品分类id
	private String productIds;//批量商品id
	private String productId;//商品id
	private String activityProUuids;//批量平台活动商品上架id
	private String productName;
	
	/**
	 * 去平台已参与商品页面
	 * @return
	 */
	public String toActivityProductListPage(){
		
		return "toActivityProductListPage";
	}
	
	/**
	 * 去添加商品页面
	 * @return
	 */
	public String toAddProductPage(){
		
		return "toAddProductPage";
	}
	
	/**
	 * 获取平台参与商品
	 * @return
	 */
	public void getProductList(){
		String userCode=getUserCodeFromSession();								//当前登录人员账号
		UserInfoBean userInfo=getUserInfo(userCode);							//登录人员信息
		String shopId=userInfo.getShopId();										//绑定店铺id
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("startIndex", getStartIndexMysql());
		map.put("endIndex", getEndIndexMysql());
		map.put("shopId", shopId);
		map.put("activityUuid", activityUuid);
		map.put("minPriceStr", minPriceStr);
		map.put("maxPriceStr", maxPriceStr);
		map.put("productName", productName);
		
		try {
			List<Map<String, Object>> list=activityProductService.getProductList(map);
			Long total=activityProductService.getProductTotal(map);
			dataGridJson=new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
			infolog.info("activityProductService|getProductList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductAction|异常：",e);
		}finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
		
	}
	
	/**
	 * 获取商品分类树
	 */
	public void getTree(){
		String userCode=getUserCodeFromSession();								//当前登录人员账号
		UserInfoBean userInfo=getUserInfo(userCode);							//登录人员信息
		String shopId=userInfo.getShopId();										//绑定店铺id
		String corpId = userInfo.getCorpId();
		
		request = getRequest();
		//id 异步加载展开树时传到后台的id
		//tarEnt 入口编码，以编码为入口编码的节点作为根节点
		//ifAll 是否一次性展示所有数据，1是 0否
		String id = request.getParameter("id");
		id = id == null ? "" : id;
		String ifAll = request.getParameter("ifAll");
		ifAll = ifAll == null ? "0" : ifAll;
		SyncSelectTree selectTree = new SyncSelectTree();
		selectTree.setId(id);
		selectTree.setIfAll(ifAll);
		List<SyncSelectTree> list = activityProductService.getTree(selectTree);
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		for (SyncSelectTree sst : list) {
			sst.setOpen(true);
			if(sst.getIsParent() && "0".equals(sst.getpId())){
				sst.setNocheck(true);
			}
		}
		str2Resp(JSONArray.fromObject(list).toString());
	}
	
	/**
	 * 根据分类code获取 活动未添加商品
	 * @return
	 */
	public void getNoAddProduct(){
		String userCode=getUserCodeFromSession();								//当前登录人员账号
		UserInfoBean userInfo=getUserInfo(userCode);							//登录人员信息
		String shopId=userInfo.getShopId();										//绑定店铺id
		
		Map<String, String> map=new HashMap<String, String>();
		map.put("startIndex", getStartIndexMysql());
		map.put("endIndex", getEndIndexMysql());
		map.put("shopId", shopId);
		map.put("categoryCode", categoryCode);
		map.put("activityUuid", activityUuid);
		
		try {
			List<Map<String, Object>> list=activityProductService.getNoAddProduct(map);
			Long total=activityProductService.getNoAddProductTotal(map);
			dataGridJson=new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
			infolog.info("activityProductService|getNoAddProduct");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductAction|异常：",e);
		}finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
		
	}
	
	/**
	 * 批量保存商品至平台活动
	 * @return
	 */
	public String saveProduct(){
		String userCode=getUserCodeFromSession();								//当前登录人员账号
		UserInfoBean userInfo=getUserInfo(userCode);							//登录人员信息
		String shopId=userInfo.getShopId();										//绑定店铺id
		
		List<Map<String, String>> list=new ArrayList<Map<String,String>>();
		List<String> stringList=stringToList(productIds);
		
		for(String productId:stringList){
			Map<String, String> m=new HashMap<String, String>();
			m.put("activityProUuid", UUID.randomUUID().toString());
			m.put("shopId", shopId);
			m.put("productId", productId);
			m.put("activityUuid", activityUuid);
			m.put("status", "1");
			m.put("approveStatus", "1");//默认审核通过
			m.put("createTime", DateUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
			list.add(m);
		}
		
		Boolean result=activityProductService.saveProduct(list);
		if(result){
			jsonText.setSuccess(true);
		}else{
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	
	/**
	 * 从活动中删除商品
	 * @return
	 */
	public String deleteProduct(){
		String userCode=getUserCodeFromSession();								//当前登录人员账号
		UserInfoBean userInfo=getUserInfo(userCode);							//登录人员信息
		String shopId=userInfo.getShopId();										//绑定店铺id
		
		Map<String, String> map=new HashMap<String, String>();
		map.put("shopId", shopId);
		map.put("productId", productId);
		map.put("activityUuid", activityUuid);
		
		Boolean result=activityProductService.deleteProduct(map);
		if(result){
			jsonText.setSuccess(true);
		}else{
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	
	/**
	 * 批量删除商品
	 * @return
	 */
	public String  deleteProducts(){
		List<String> activityProUuidsList=stringToList(activityProUuids);
		
		Boolean result=activityProductService.deleteProducts(activityProUuidsList);
		if(result){
			jsonText.setSuccess(true);
		}else{
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	
	/**
	 * 根据逗号 把字符串转换为list
	 * @param string
	 * @return
	 */
	private List<String> stringToList(String string){
        String d[] = string.split(",");
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < d.length; i++) {
        	list.add(d[i]);
        }
		return list;
		
	}

	public String getActivityUuid() {
		return activityUuid;
	}

	public void setActivityUuid(String activityUuid) {
		this.activityUuid = activityUuid;
	}

	public String getMinPriceStr() {
		return minPriceStr;
	}

	public void setMinPriceStr(String minPriceStr) {
		this.minPriceStr = minPriceStr;
	}

	public String getMaxPriceStr() {
		return maxPriceStr;
	}

	public void setMaxPriceStr(String maxPriceStr) {
		this.maxPriceStr = maxPriceStr;
	}

	public String getCategoryCode() {
		return categoryCode;
	}

	public void setCategoryCode(String categoryCode) {
		this.categoryCode = categoryCode;
	}

	public String getProductIds() {
		return productIds;
	}

	public void setProductIds(String productIds) {
		this.productIds = productIds;
	}

	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getActivityProUuids() {
		return activityProUuids;
	}

	public void setActivityProUuids(String activityProUuids) {
		this.activityProUuids = activityProUuids;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}
	

}
